package com.ajie.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.ajie.config.annotation.RestControllerMapping;
import com.ajie.entity.SysUser;
import com.ajie.model.Result;
import com.ajie.service.SysUserService;
import com.ajie.utils.PasswordUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ajie
 * @createTime: 2024-07-20 15:20
 */
@Tag(name = "当前登录用户")
@RestControllerMapping("currentUser")
public class CurrentUserController {

    @Resource
    private SysUserService userService;

    @GetMapping("info")
    @Operation(summary = "登录用户信息")
    public Result<SysUser> info() {
        String userId = StpUtil.getLoginIdAsString();
        SysUser user = userService.getById(userId);
        user.setPassword(null);
        user.setSlat(null);
        return Result.success(user);
    }

    @GetMapping("menuList")
    @Operation(summary = "用户菜单列表")
    public Result<List<JSONObject>> menuList() {
        SaSession saSession = StpUtil.getSessionByLoginId(StpUtil.getLoginId());
        List<JSONObject> menus = saSession.get("menu", () -> userService.listMenu());
        return Result.success(menus);
    }

    @GetMapping("clearCache")
    @Operation(summary = "清除缓存")
    public Result<Boolean> clearCache() {
        SaSession session = StpUtil.getSession();
        session.clear();
        return Result.success(true);
    }

    @GetMapping("getPerms")
    @Operation(summary = "获取当前登录用户的角色列表和权限列表")
    public Result<Map<String, Object>> getPerms() {
        Map<String, Object> map = new HashMap<>(2);
        List<String> roles = StpUtil.getSession().get("Role_List", userService.listRoleByUserId(StpUtil.getLoginIdAsString()));
        List<String> perms = new ArrayList<>();
        for (String roleCode : roles) {
            SaSession session = SaSessionCustomUtil.getSessionById("role-" + roleCode);
            List<String> permissions = session.get("Permission_List", () -> userService.listPermissionByRoleCode(roleCode));
            perms.addAll(permissions);
        }
        map.put("roles", roles);
        map.put("perms", perms);
        return Result.success(map);
    }


    @PostMapping("updateUser")
    public Result<Boolean> updateUser(@RequestBody SysUser user) {
        user.setId(StpUtil.getLoginIdAsString());
        if (StrUtil.isNotBlank(user.getPassword())) {
            String slat = RandomUtil.randomString(16);
            user.setSlat(slat);
            String password = user.getPassword();
            String encrypt = PasswordUtils.encrypt(slat, password);
            user.setPassword(encrypt);
        }
        if (userService.updateById(user)) {
            return Result.success();
        }
        return Result.error();
    }
}
